package de.uni.muenster.idw2.location.bluetooth.service.fingerprinting.measurements;

import java.util.ArrayList;
import java.util.List;

/**
 * Measured dongle.
 * 
 * @author Eva Peters
 *
 */
public class Dongle
{
	/**
	 * The name.
	 */
	private String name;
	
	/**
	 * The positive signal strengths in decibel.
	 */
     //Luis changed this
	//private List<Integer> positiveSignalStrengths = new ArrayList<Integer>();
	
	/**
	 * The maximum possible signal strength.
	 */
	private final int MAXIMUM_POSSIBLE_SIGNAL_STRENGTH = 255;
	
	private int count = 0;
	private int mean = 0;
	private int minimum = MAXIMUM_POSSIBLE_SIGNAL_STRENGTH;
	private int maximum = 0; //Luis -> Eva: For keeping your semantics
	
	/**
	 * Constructor.
	 * 
	 * @param name The name of the dongle
	 */
	public Dongle(String name)
	{
		this.name = name;
	}
	
	/**
	 * Adds a measured signal strength to the list of signal strengths.
	 * 
	 * The given negative signal strength is converted to a positive value.
	 * 
	 * @param signalStrength The negative signal strength in decibel (dB).
	 */
	public void addSignalStrength(int signalStrength)
	{
		int signalStrengthAbs = Math.abs(signalStrength);
		
		//update minimum 
		if (signalStrengthAbs < minimum ){
			minimum = signalStrengthAbs;
		}
		
		//update maximum 
		if (signalStrengthAbs > maximum) {
			maximum  = signalStrengthAbs;
		}
		
		//update mean .. actually a sum
        mean += signalStrengthAbs;  
		//increment count;
		count++;
		//positiveSignalStrengths.add(signalStrengthAbs);
	}

	/**
	 * Gets the name.
	 * 
	 * @return The name.
	 */
	public String getName()
	{
		return name;
	}
	
	/**
	 * Gets the minimum signal strength.
	 * 
	 * Returns the {@link #MAXIMUM_POSSIBLE_SIGNAL_STRENGTH} 
	 * if the list of signal strengths ({@link #positiveSignalStrengths}) is empty.
	 * 
	 * @return The minimum signal strength
	 */
	public int getMinimum()
	{
		/*int minimum = MAXIMUM_POSSIBLE_SIGNAL_STRENGTH;
		for(int positiveSignalStrength : positiveSignalStrengths)
		{
			if(positiveSignalStrength < minimum)
			{
				minimum = positiveSignalStrength;
			}
		}
		*/
		return minimum;
	}
	
	/**
	 * Gets the maximum signal strength.
	 * 
	 * Returns 0
	 * if the list of signal strengths ({@link #positiveSignalStrengths}) is empty.
	 * 
	 * @return The maximum signal strength
	 */
	public int getMaximum()
	{
		/*int maximum = 0;
		for(int positiveSignalStrength : positiveSignalStrengths)
		{
			if(positiveSignalStrength > maximum)
			{
				maximum = positiveSignalStrength;
			}
		}*/
		
		return maximum;
	}
	
	/**
	 * Gets the average signal strength.
	 * 
	 * Returns the {@link #MAXIMUM_POSSIBLE_SIGNAL_STRENGTH} 
	 * if the list of signal strengths ({@link #positiveSignalStrengths}) is empty.
	 * 
	 * @return The average signal strength
	 */
	public int getMean()
	{
		/*int sum = 0;
		int number = 0;
		for(int signalStrength : positiveSignalStrengths)
		{
			sum += signalStrength;
			number++;
		}
		
		int mean = MAXIMUM_POSSIBLE_SIGNAL_STRENGTH;
		if(number == 0)
		{
			return mean;
		}
		else
		{
			mean = (int) ((double)sum / (double)number);
			return mean;
		}*/
		//Luis: if no mean is accumulated i.e 0 elements return Maximum otherwise the accumulated mean.
		return (count==0)? MAXIMUM_POSSIBLE_SIGNAL_STRENGTH : (int)(mean/count);
	}
}
